package com.haruhi.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {

    @Autowired
    private RedisTemplate redisTemplate;

    public void set(String key,Object value){
        redisTemplate.opsForValue().set(key,value);
    }

    /**
     * set字符串
     * @param key
     * @param value
     * @param time 时间/秒
     */
    public void set(String key,Object value,long time){
        redisTemplate.opsForValue().set(key,value,time, TimeUnit.SECONDS);
    }
    public Object get(String key){
        return redisTemplate.opsForValue().get(key);
    }
    /**
     * 根据 key 获取过期时间
     * @param key 键 不能为null
     * @return 时间(秒) 返回-1代表为永久有效 -2表示key不存在
     */
    public long getExpire(Object key){
        return redisTemplate.getExpire(key,TimeUnit.SECONDS);
    }

}
